package auth

import (
	"go.uber.org/zap"
	"net/http"
	"gitee.com/viwii/vconf/frame"
	"gitee.com/viwii/vconf/utils"

	"github.com/ant0ine/go-json-rest/rest"
)

// AuthenticatorFunc jwt认证
func AuthenticatorFunc(userID string, password string) bool {
	return true;
	//return userID == "admin" && password == "admin"
}

// Auth 登陆相关
type Auth struct {
	frame.BaseHandler
}

// gAuthInstance 单例实例
var gAuthInstance *Auth

type WeChatReQ struct {
	OpenId     string `json:"openid"`
	SessionKey string `json:"session_key"`
	UnionId    string `json:"union_id"`
	ErrCode    int    `json:"err_code"`
	ErrMsg     string `json:"err_msg"`
}

// WeChatLogin 登陆
func (ah *Auth) WechatLogin(userId int32,w rest.ResponseWriter, req *rest.Request) error{
	var wechat WeChatReQ
	err := req.DecodeJsonPayload(&wechat)
	if(err == nil){
		return err
	}

	//dbProxy := db.NewDBProxy()
	//
	//db := dbProxy.Get(db.DBAccountDB)
	//var account dao.Accountsinfo
	//
	//err = db.Where("Account = ?",wechat.UnionId).Limit(20, 10).Find(&account)
	//log.Println(err, account)
	//strData, err := json.Marshal(data)

	if ah.BaseHandler.AuthMiddleware != nil {
		ah.BaseHandler.AuthMiddleware.LoginHandler(wechat.OpenId, wechat.SessionKey, w, req)
	}


	return nil
}

type GuestLoginReq struct{
	Password string `json:"password"`
	UUID string `json:"UUID"`
}

// GuestLogin 游客登陆
func (ah *Auth) GuestLogin(userId int32, w rest.ResponseWriter, req *rest.Request) error {
	log := utils.Logger()
	var guest GuestLoginReq
	err := req.DecodeJsonPayload(&guest)
	if err != nil{
		return err
	}


	if len(guest.UUID) <5 || len(guest.Password) < 5{
		log.Info("GuestLogin Invalid string:",zap.String("uuid",guest.UUID),zap.String("password",guest.Password))
		return nil
	}

	//dbProxy := db.NewDBProxy()
	//
	//engine := dbProxy.Get(db.DBAccountDB)
	//playerInfo:= new(dao.PlayerInfo)
	//has,err := engine.Where("Account = ?",guest.UUID).Get(playerInfo)
	////.Table(&account).Fields("PlayerID").Where("Account", "=", guest.UUID).Get()
	////fmt.Println(err,has)
	//if !has { // 用户不存在注册新用户
	//	playerInfo.Account = guest.UUID
	//	playerInfo.RegisterDate = time.Now()
	//	playerInfo.LastLoginDate = time.Now()
	//	playerInfo.Password = guest.Password
	//
	//	_,err:=engine.Insert(playerInfo)
	//	if err!= nil {
	//		log.Info("insert into playerInfo table err:",zap.Error(err))
	//	}else{
	//		nickName:= fmt.Sprintf("s%d",int(playerInfo.PlayerId))
	//		_,err=engine.ID(playerInfo.PlayerId).Cols("NickName").Update(&dao.PlayerInfo{NickName:nickName})
	//		if err != nil{
	//			log.Info("update nickName error:",zap.Error(err))
	//		}
	//
	//	}
	//
	//} else if err != nil {
	//	log.Info("Query PlayerInfo err:",zap.Error(err))
	//	w.WriteHeader(http.StatusOK)
	//
	//	return nil
	//} else if (playerInfo.Password != guest.Password){
	//	w.WriteHeader(http.StatusOK)
	//	log.Info("GuestLogin PassWord err:")
	//
	//	return nil
	//}
	//
	//fmt.Println(playerInfo.Password,guest.Password)
	//
	//if ah.BaseHandler.AuthMiddleware != nil {
	//	ah.BaseHandler.AuthMiddleware.LoginHandler(strconv.Itoa(int(playerInfo.PlayerId)), guest.Password, w, req)
	//}

	w.WriteHeader(http.StatusOK)

	return nil
}


func NewAuth(middle *frame.JWTMiddleware) *Auth {
	if gAuthInstance == nil {
		gAuthInstance = new(Auth)

		gAuthInstance.InitBase(middle,"auth",gAuthInstance)
	}

	return gAuthInstance
}
